package com.jun.sso.service.impl;

import com.jun.common.core.mapper.AdminUserMapper;
import com.jun.common.core.model.AdminUser;
import com.jun.sso.core.entity.ReturnT;

import com.jun.sso.model.UserInfo;
import com.jun.sso.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private AdminUserMapper adminUserMapper;

    /*private static List<UserInfo> mockUserList = new ArrayList<>();
    static {
        for (int i = 0; i <5; i++) {
            UserInfo userInfo = new UserInfo();
            userInfo.setUserid(1000+i);
            userInfo.setUsername("user" + (i>0?String.valueOf(i):""));
            userInfo.setPassword("123456");
            mockUserList.add(userInfo);
        }
    }*/

    @Override
    public ReturnT<UserInfo> findUser(String username, String password) {

        if (username==null || username.trim().length()==0) {
            return new ReturnT<UserInfo>(ReturnT.FAIL_CODE, "Please input username.");
        }
        if (password==null || password.trim().length()==0) {
            return new ReturnT<UserInfo>(ReturnT.FAIL_CODE, "Please input password.");
        }

        //TODO 明文登录
        AdminUser adminUser = adminUserMapper.selectByAccountByPassword(username, password);

        // 查询到则返回
        if(Objects.nonNull(adminUser)){
            UserInfo userInfo = new UserInfo();
            userInfo.setUsername(adminUser.getAccount());
            userInfo.setPassword(adminUser.getPassword());
            userInfo.setUserId(adminUser.getUserId());
            return new ReturnT<UserInfo>(userInfo);
        }

        return new ReturnT<UserInfo>(ReturnT.FAIL_CODE, "username or password is invalid.");
    }


}
